﻿@inherits AdminCompontentBase
<SSimpleModal Value="_visible" ValueChanged="HandleVisibleChanged" OnDelete="HandleDel" OnCancel="HandleCancel" OnSave="HandleOk"
              Title="@T(_entityId == default?"AddAlarmRule":"EditAlarmRule")" Class="full-height" HeaderClass="mr-4">
    <ChildContent>
        <MForm Model="_model" EnableValidation @ref="_form" Class="full-height mt-n2 pt-3">
            <MStepper Value="_model.Step" Elevation=0 Class="d-flex flex-column full-height">
                <MStepperHeader Class="ma-n6" Style="box-shadow:none !important">
                    <MStepperStep Step="1" Complete="_model.Step>1">
                        @T("MonitoringSetting")
                    </MStepperStep>
                    <MDivider></MDivider>
                    <MStepperStep Step="2">
                        @T("AlarmSetting")
                    </MStepperStep>
                </MStepperHeader>
                <MStepperItems Class="full-height">
                    <MStepperContent Class="full-height pa-0" Step="1">
                        <AutoHeight Overflow Class="mt-9 pb-9">
                            <AutoHeightContent>
                                <DefaultTitle Class="mb-6">
                                    @T("ChartSetting")
                                    <DefaultTooltip Class="ml-1 mt-n1" Tooltip="@T("ChartSettingTip")" />
                                </DefaultTitle>
                                <STextField @bind-Value="_model.ChartYAxisUnit" Label="@T(nameof(_model.ChartYAxisUnit))" />
                                <DefaultTitle>@T("VariableConfiguration")</DefaultTitle>
                                <MRow NoGutters>
                                    <MCol Md="4">
                                        <SSelect @bind-Value="_model.CheckFrequency.Type" Items="Enum.GetValues<AlarmCheckFrequencyTypes>().ToList()" ItemText="e => T(e==default?string.Empty:e.ToString())" ItemValue="e=> e" Label="@T(nameof(_model.CheckFrequency))" />
                                    </MCol>
                                    @if (_model.CheckFrequency.Type == AlarmCheckFrequencyTypes.FixedInterval)
                                    {
                                        <MCol Md="4">
                                            <STextField Class="ml-6" @bind-Value="_model.CheckFrequency.FixedInterval.IntervalTime" Label="@T(nameof(_model.CheckFrequency.FixedInterval.IntervalTime))" />
                                        </MCol>
                                        <MCol Md="4">
                                            <SSelect Class="ml-6" @bind-Value="_model.CheckFrequency.FixedInterval.IntervalTimeType" Label="@T(nameof(_model.CheckFrequency.FixedInterval.IntervalTimeType))" Items="Enum.GetValues<TimeTypes>().ToList()" ItemText="e => T(e==default?string.Empty:e.ToString())" ItemValue="e=> e" />
                                        </MCol>
                                    }
                                    @if (_model.CheckFrequency.Type == AlarmCheckFrequencyTypes.Cron)
                                    {
                                        <MCol Md="8">
                                            <STextField Class="ml-6" @bind-Value="_model.CheckFrequency.CronExpression" Label="@T(nameof(_model.CheckFrequency.CronExpression))" AppendIcon="mdi-clock" OnAppendClick="OpenCronModal" OnChange="(string v)=>GetNextRunTime()" />
                                        </MCol>
                                    }
                                </MRow>
                                @if (_model.CheckFrequency.Type == AlarmCheckFrequencyTypes.Cron && !string.IsNullOrEmpty(_model.CheckFrequency.CronExpression))
                                {
                                    <div class="mt-4 caption regular3--text rounded-lg bgray">
                                        <p class="emphasis px-5 rounded-t-lg white--text py-2 text-body-2">
                                            <span class="mdi mdi-clock-outline mr-1 clock-size"></span>
                                            @T("NextRunTimeTip")
                                        </p>
                                        @((MarkupString)_nextRunTimeStr)
                                    </div>
                                }
                                @*<MRow Class="my-8">
                                    <MSwitch @bind-Value="_model.IsGetTotal" Inset Class="label my-auto ml-4" Label="@T("GetTotal")" />
                                    @if (_model.IsGetTotal)
                                    {
                                    <MCol Md="2">
                                    <STextField @bind-Value="_model.TotalVariable" Label="@T(nameof(_model.TotalVariable))" />
                                    </MCol>
                                    }
                                </MRow>*@
                                @foreach (var item in _model.MetricMonitorItems)
                                {
                                    <MBorder Color="green" Width="4" Class="rounded-tl-lg rounded-bl-lg" Style="height: calc(100% - 16px);">
                                        <MSwitch @bind-Value="item.IsExpression" Inset Class="label mt-9 ml-3" Label="@T("Expression")">
                                        </MSwitch>
                                        @if (item.IsExpression)
                                        {
                                            <STextarea Class="mt-6 ml-3" @bind-Value="item.Expression" Label="@T(nameof(item.Expression))" Outlined HideDetails="@("auto")"></STextarea>
                                        }
                                        else
                                        {
                                            <MRow NoGutters Class="ml-3 mt-6">
                                                <MCol Md="3">
                                                    <SAutoComplete Items="_names" @bind-Value="item.Aggregation.Name"
                                                           Label="@T("MetricMonitor.Name")"
                                                           ItemText="v => v"
                                                           ItemValue="v=> v"
                                                           Dense
                                                           Outlined
                                                           Clearable
                                                           Flat
                                                           Solo
                                                           HideDetails="@("auto")"
                                                           TValue="string" 
                                                           TItem="string" 
                                                           TItemValue="string"
                                                           Class="mr-6 rounded-2 m-input--h-40"
                                                           Filter="(item,search,value) => true"
                                                           OnSearchInputUpdate="QueryNames"
                                                           OnSelectedItemUpdate="(v)=>HandleMetricNameChange(v,item)">
                                                           <SelectionContent Context="data">
                                                              <SAutoCompleteSelection Value="@item.Aggregation.Name"  Style="height:40px;" />
                                                           </SelectionContent>                             
                                                    </SAutoComplete>
                                                </MCol>
                                                <MCol Md="3">
                                                    <SSelect Class="ml-3" @bind-Value="item.Aggregation.Tag" Items="item.Aggregation.TagItems" ItemText="v => v" ItemValue="v=> v" TValue="string" TItem="string" TItemValue="string" Label="@T("MetricMonitor.Tag")" OnChange="(v)=>HandleMetricTagChange(v,item)" />
                                                </MCol>
                                                <MCol Md="2">
                                                    <SSelect Class="ml-3" @bind-Value="item.Aggregation.ComparisonOperator" Label="@T("MetricMonitor.ComparisonOperator")" Items="Enum.GetValues<MetricComparisonOperators>().ToList()" ItemText="e => T(e==default?string.Empty:e.ToString())" ItemValue="e=> e" />
                                                </MCol>
                                                <MCol Md="2">
                                                    <SSelect Class="ml-3" @bind-Value="item.Aggregation.Value" Label="@T("MetricMonitor.Value")" Items="item.Aggregation.ValueItems" ItemText="v => v" ItemValue="v=> v" />
                                                </MCol>
                                                <MCol Md="2">
                                                    <SSelect Class="ml-3" @bind-Value="item.Aggregation.AggregationType" Label="@T("MetricMonitor.AggregationType")" Items="Enum.GetValues<MetricAggregationTypes>().ToList()" ItemText="e => T(e==default?string.Empty:e.ToString())" ItemValue="e=> e" />
                                                </MCol>
                                            </MRow>
                                        }
                                        <MRow NoGutters Class="ml-3 mt-6">
                                            <MCol Md="3">
                                                <STextField @bind-Value="item.Alias" Label="@T("MetricMonitor.Alias")">
                                                    <PrependContent>
                                                        <span class="regular2--text body2 my-auto ml-2">@T("as")</span>
                                                    </PrependContent>
                                                </STextField>
                                            </MCol>
                                            <MSwitch @bind-Value="item.IsOffset" Inset Class="label mt-2 ml-3">
                                                <LabelContent>
                                                    <DefaultTooltip Class="ml-2 mt-n1" Tooltip="@T("ChartSettingTip")" />
                                                    <span class="regular2--text body2 my-auto ml-1">@T("Offset")</span>
                                                </LabelContent>
                                            </MSwitch>
                                            @if (item.IsOffset)
                                            {
                                                <MCol Md="3">
                                                    <STextField @bind-Value="item.OffsetPeriod" Label="@T(nameof(item.OffsetPeriod))" Class="ml-3">
                                                        <AppendOuterContent>
                                                            <span class="regular2--text body2 my-auto">@T("Period")</span>
                                                        </AppendOuterContent>
                                                    </STextField>
                                                </MCol>
                                            }
                                        </MRow>
                                        <MDivider Right Class="mt-6 ml-3">
                                            <MButtonGroup Dense Rounded Class="mt-n5">
                                                <MButton Color="fill" IsActive="false" OnClick="()=>HandleMetricMonitorItemsAdd(item)">
                                                    <SIcon Size="20" Color="emphasis">mdi-plus</SIcon>
                                                </MButton>
                                                @if (_model.MetricMonitorItems.Count > 1)
                                                {
                                                    <MButton Color="fill" IsActive="false" OnClick="()=>HandleMetricMonitorItemsRemove(item)">
                                                        <SIcon Size="20" Color="emphasis">mdi-delete</SIcon>
                                                    </MButton>
                                                }
                                            </MButtonGroup>
                                        </MDivider>
                                    </MBorder>
                                }
                            </AutoHeightContent>
                        </AutoHeight>
                    </MStepperContent>
                    <MStepperContent Class="full-height pa-0" Step="2">
                        <AutoHeight Overflow Class="mt-9 pb-9">
                            <AutoHeightContent>
                                <AlarmRuleSetting @bind-Model="_model"></AlarmRuleSetting>
                            </AutoHeightContent>
                        </AutoHeight>
                    </MStepperContent>
                </MStepperItems>
            </MStepper>
        </MForm>
    </ChildContent>
    <DeleteContent Context="delete">
        @if (_model.Step == 1)
        {
            <MButton Class="mr-4" Icon OnClick="()=>_previewChart?.OpenModalAsync()!">
                <SIcon Tooltip="@T("ChartPreview")" Size="24" Color="primary" Class="align-self-center">mdi-chart-line</SIcon>
            </MButton>

            @if (_entityId != default)
            {
                <span class="d-flex px-6" style="height: 12px">
                    <MDivider Vertical />
                </span>
                <MButton Class="mr-4" Icon OnClick="delete.Click">
                    <SIcon Size="24" Color="error" Class="align-self-center">mdi-delete</SIcon>
                </MButton>
            }
        }
        @if (_model.Step == 2)
        {
            <EnableSwitch Class="d-inline-flex" @bind-Value="_model.IsEnabled" />
            <span class="d-flex px-6" style="height: 12px">
                <MDivider Vertical />
            </span>
            <MButton Class="mr-4" Icon OnClick="()=>_previewChart?.OpenModalAsync()!">
                <SIcon Tooltip="@T("ChartPreview")" Size="24" Color="primary" Class="align-self-center">mdi-chart-line</SIcon>
            </MButton>

            @if (_entityId != default)
            {
                <span class="d-flex px-6" style="height: 12px">
                    <MDivider Vertical />
                </span>
                <MButton Class="mr-4" Icon OnClick="delete.Click">
                    <SIcon Size="24" Color="error" Class="align-self-center">mdi-delete</SIcon>
                </MButton>
            }
        }
    </DeleteContent>
    <SaveContent Context="save">
        @if (_model.Step == 1)
        {
            <SButton BorderRadiusClass="rounded-3" Class="btn-form ml-6" OnClick="()=> HandleNextStep()">@T("NextStep")</SButton>
        }
        @if (_model.Step == 2)
        {
            <SButton BorderRadiusClass="rounded-3" Color="fill" Class="btn-form line-primary btn primary--text" OnClick="()=>_model.Step=1">@T("PreviousStep")</SButton>
            <SButton BorderRadiusClass="rounded-3" Class="btn-form ml-6" OnClick="HandleOk">@T(_entityId == default?"Submit":"Save")</SButton>
        }
    </SaveContent>
</SSimpleModal>

<SSimpleModal @bind-Value="@_cronVisible" Title="@T("CronExpression")" OnSave="SetCronExpression">
    <PCron @bind-Value="@(_tempCron)">
    </PCron>
</SSimpleModal>

<AlarmPreviewChartModal @ref="_previewChart" AlarmRule="_model" />